<template>
  <div class="search">
    <div class="search-box-wrapper">
      <search-box ref="searchBoxRef" @query="changeQyery"></search-box>
    </div>
    <div class="shortcut-wrapper" v-show="!query" ref="shortcutWraper">
      <scroll class="shortcut" :refreshDelay="refreshDelay" :data="vcutData" ref="shortcut">
        <div>
          <div class="hot-key">
            <h1 class="title">热门搜索</h1>
            <ul>
              <li class="item" v-for="item in hotkey" :key="item.n" @click="changeSearch(item.k)">{{item.k}}</li>
            </ul>
          </div>
          <!-- 搜索历史 -->
          <div class="search-history" v-show="searchHistory.length>0">
            <h1 class="title">
              <span class="text">搜索历史</span>
              <span class="clear" @click="allClear">
                <i class="icon-clear"></i>
              </span>
            </h1>
            <search-list :searches="searchHistory" @select="changeSearch" @delateHistory="delateSearchHistory"></search-list>
          </div>
        </div>
      </scroll>
    </div>
    <div class="search-result" v-show="query" ref="result">
      <sugest :query="query" @changeScroll="changeScroll" @select="saveSearch" ref="sugestref"></sugest>
    </div>
    <confirm ref="confirm" text="是否清空搜索历史" @trueBtnFne="allSearchHistory"></confirm>
    <router-view></router-view>
  </div>
</template>

<script>
  import SearchBox from 'base/search-box/SearchBox';
  import {
    getHotSearch
  } from 'api/search';
  import Sugest from 'components/sugest/Sugest';
  import {
    mapActions,
    // mapGetters
  } from 'vuex';
  // import SearchList from 'base/search-list';
  import Confirm from 'base/confirm';
  import Scroll from 'base/scroll/scroll';
  import {
    playlistMixin,
    searchMixin
  } from 'common/js/mixin';

  export default {
    name: 'Search',
    mixins: [playlistMixin, searchMixin],
    components: {
      SearchBox,
      Sugest,
      // SearchList,
      Confirm,
      Scroll
    },
    data() {
      return {
        hotkey: [],
      }
    },
    computed: {
      vcutData() {
        // concat() 方法用于连接两个或多个数组。
        return this.hotkey.concat(this.searchHistory)
      }
    },
    methods: {
      getHotData() {
        getHotSearch().then((res) => {
          if (res.code == 0) {
            this.hotkey = res.data.hotkey.slice(0, 10)
          }
        })
      },
      allClear() {
        this.$refs.confirm.show()
      },
      handlePlayList(playList) {
        const bottom = playList.length > 0 ? '60px' : ''

        this.$refs.shortcutWraper.style.bottom = bottom
        this.$refs.shortcut.refresh()

        this.$refs.result.style.bottom = bottom
        this.$refs.sugestref.refresh()
      },
      ...mapActions(['allSearchHistory'])
    },
    watch: {
      query(newQuery) {
        if (!newQuery) {
          setTimeout(() => {
            this.$refs.shortcut.refresh()
          }, 20)
        }
      }
    },
    mounted() {
      this.getHotData()
    },
  }

</script>
